//
//  SetPwdView.swift
//  casdon_swiftUI
//
//  Created by ww on 2025/5/18.
//

import SwiftUI
import FlowStacks

struct SetPwdView: View {
    @StateObject private var viewModel: SetPwdViewModel

    @EnvironmentObject var navigator: FlowNavigator<LoginStack>
    
    let leftOffset = 24.0
    
    init(viewModel: SetPwdViewModel) {
        _viewModel = StateObject(wrappedValue: viewModel)
    }
    
    var body: some View {
        VStack {
            NavigationBar(title: "")
            HStack{
                Text("设置密码")
                    .font(.system(size: 24))
                    .foregroundColor(AppColors.Gy1)
                    .bold()
                Spacer()
            }.padding(.leading, 24)
                .padding(.top, 20)
            
            VStack {
                // 账号输入框
                CustomSecureField(
                    text: $viewModel.password1,
                    placeholder: "password",
                    isSecure: $viewModel.isPasswordHidden
                )
                .disabled(viewModel.isLoading)
                // 密码输入框
                CustomSecureField(
                    text: $viewModel.password2,
                    placeholder: "password",
                    isSecure: $viewModel.isPasswordHidden
                )
                .disabled(viewModel.isLoading)
            }
            .padding(.horizontal, leftOffset)
            .padding(.top, 48)
            
            HStack {
                Button(action: {
                    viewModel.execute()
                }) {
                    ZStack {
                        if viewModel.isLoading {
                            ProgressView()
                                .progressViewStyle(CircularProgressViewStyle(tint: .white))
                        } else {
                            Text("login")
                                .font(.system(size: 16, weight: .medium))
                        }
                    }
                    .frame(maxWidth: .infinity)
                    .frame(height: 44)
                    .background(viewModel.verifyPwd() && !viewModel.isLoading ? AppColors.Brand6 : AppColors.Brand6.opacity(0.5))
                    .cornerRadius(6)
                    .foregroundColor(.white)
                    .contentShape(Rectangle())
                }
                .disabled(viewModel.isLoading || !viewModel.verifyPwd())
                .padding(.horizontal, leftOffset)
            }.padding(.top, 25)
            Spacer()
        }
        .onChange(of: viewModel.executeSucces) { success in
            if success {
                navigator.popTo(.loginPwd)
            }
        }

    }
}
